import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Login {

	private static JPanel contentPane;
	private static JTextField tfPort;
	private static JTextField tfIP;
	private static JButton btnConnect;
	private static JButton btnExit;
	private static JFrame frame;
	private static ObjectInputStream ois;
	private static ObjectOutputStream oos;

	public static void main(String[] args) {
		createAndShowGUI();
	}

	private static void createAndShowGUI() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		frame = new JFrame("Login");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(null);
		contentPane = (JPanel) frame.getContentPane();

		JLabel lbPort = new JLabel("Port : ");
		lbPort.setBounds(10, 10, 80, 25);
		contentPane.add(lbPort);

		tfPort = new JTextField();
		tfPort.setBounds(100, 10, 150, 25);
		contentPane.add(tfPort);

		JLabel lbIP = new JLabel("IP : ");
		lbIP.setBounds(10, 50, 80, 25);
		contentPane.add(lbIP);

		tfIP = new JTextField();
		tfIP.setBounds(100, 50, 150, 25);
		contentPane.add(tfIP);

		btnConnect = new JButton("Connect");
		btnConnect.setBounds(10, 90, 100, 25);
		btnConnect.setActionCommand("btnConnect");
		btnConnect.addActionListener(new ButtonListener());
		contentPane.add(btnConnect);

		btnExit = new JButton("Exit");
		btnExit.setBounds(150, 90, 100, 25);
		btnExit.setActionCommand("btnExit");
		btnExit.addActionListener(new ButtonListener());
		contentPane.add(btnExit);

		frame.pack();
		frame.setSize(new Dimension(270, 155));
		frame.setResizable(false);

		frame.setVisible(true);
	}

	private static class ButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String str = e.getActionCommand();
			if (str.equals("btnConnect")) {
				try {
					Socket s = new Socket(tfIP.getText(), Integer.parseInt(tfPort.getText()));
					InputStream is = s.getInputStream();
					ois = new ObjectInputStream(is);
					OutputStream os = s.getOutputStream();
					oos = new ObjectOutputStream(os);
					Client.runClient(frame, ois,oos);
					frame.setVisible(false);
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, "Kết nối thất bại!");
				}
			} else if (str.equals("btnExit")) {
				System.exit(0);
			}
		}
	}
}